package app.web.visual.disk;

import app.common.dao.Leo_disk_infoBean;
import app.esb.common.NumberTools;
import app.esb.disk.DiskExcutor;
import cn.lily.com.exception.YaconException;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.sql.TSqlGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 磁盘信息管理
 * @author JiaJ
 * @date 2017年11月7日
 *
 */
public class Disk extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TSqlGrid diskGrid = sqlGrid("diskGrid");
		diskGrid.setDictTable("LEO_DISK_INFO");

		String sql = " select t0.DISK_ID,t0.DISK_NAM,t0.DISK_SIZE,t0.DISK_TYP"
					+ ",t0.DISK_COLOR,t0.DISK_USE_SIZE,t0.DISK_FREE_SIZE,t0.DISK_PERSENT"
					+ ",t0.DISK_ACTIVE_SIZE,t0.DISK_ACTIVE_USE,t0.DISK_STATUS "
					+ " from ${schema}.LEO_DISK_INFO t0 "
					+ " order by DISK_ID";
		diskGrid.setCommSqlStr(sql);

		diskGrid.setEdit(true);
		diskGrid.setRefreshMethod("refresh");
		diskGrid.setExcuteMethod("save");

		//LEO_DISK_INFO
		diskGrid.createFieldTitle("DISK_ID").setDataAlign("center");
		diskGrid.createFieldTitle("DISK_NAM").setDataAlign("center");
		diskGrid.createFieldTitle("DISK_SIZE").setDataAlign("center");
		diskGrid.createFieldTitle("DISK_TYP").setFieldVisible(false);
		diskGrid.createFieldTitle("DISK_COLOR").setFieldVisible(false);
		diskGrid.createFieldTitle("DISK_USE_SIZE").setDataAlign("center");
		diskGrid.createFieldTitle("DISK_FREE_SIZE").setFieldVisible(false);
		diskGrid.createFieldTitle("DISK_PERSENT").setDataAlign("center");
		diskGrid.createFieldTitle("DISK_ACTIVE_SIZE").setFieldVisible(false);
		diskGrid.createFieldTitle("DISK_ACTIVE_USE").setFieldVisible(false);
		diskGrid.createFieldTitle("DISK_STATUS").setFieldVisible(false);

		diskGrid.detailPage().setPagePath("diskEdit.jsp");
		
		TButtonPilot diskGridPilot = ButtonPilot("diskGridPilot");
		diskGridPilot.setPilotIco("glyphicon glyphicon-th");
		diskGridPilot.setPilotText("物理磁盘信息");

		TButton add = new TButton("add");
		TButton edit = new TButton("edit");
		TButton delete = new TButton("delete");
		TButton refresh = new TButton("refresh");

		diskGridPilot.addButtons(add);
		diskGridPilot.addButtons(edit);
		diskGridPilot.addButtons(delete);
			
		//磁盘重置
		TButton resetBtn = Button("diskDataReset");
		resetBtn.setIcoPath("glyphicon glyphicon-share-alt");
		resetBtn.setLabel("重置");
		resetBtn.setViewClick("dataReset_onclick");
		diskGridPilot.addButtons(resetBtn);

		diskGridPilot.addButtons(refresh);
		
		diskGrid.setButtonPilot(diskGridPilot);
		
		diskGrid.setTopButtonType("all");

		this.getPage().addElement(diskGrid);
	
	}
	
	 public void refresh() throws YaconException {

		TSqlGrid diskGrid = sqlGrid("diskGrid");
		diskGrid.setDictTable("LEO_DISK_INFO");

		String sql = " select t0.DISK_ID,t0.DISK_NAM,t0.DISK_SIZE,t0.DISK_TYP"
					+ ",t0.DISK_COLOR,t0.DISK_USE_SIZE,t0.DISK_FREE_SIZE,t0.DISK_PERSENT"
					+ ",t0.DISK_ACTIVE_SIZE,t0.DISK_ACTIVE_USE,t0.DISK_STATUS "
					+ " from ${schema}.LEO_DISK_INFO t0 "
					+ " order by DISK_ID";
		diskGrid.setCommSqlStr(sql);
			
		this.setSelfMessage(diskGrid.getDataElement());

	 }

	 public void save() throws YaconException {

		 Leo_disk_infoBean diskBean = new Leo_disk_infoBean();
		 
		 while (getViewTable().next()) {
			 diskBean.setViewRecord(getViewTable().getRecord());

			 if (getViewTable().getRecord().getRecordType() == 1) {
				 
				 //磁盘名称校验				 
				 this.dataExistCommon("LEO_DISK_INFO", "DISK_NAM", "磁盘名称已经存在！");
				 
				 this.dataNullException("DISK_NAM", "磁盘名称不能为空！");
				 
				 //其他信息
				 diskBean.setDisk_use_size("0MB");  //使用大小
				 diskBean.setDisk_free_size(this.getViewTable().getString("DISK_SIZE"));  //空闲大小
				 diskBean.setDisk_persent("0.00%"); //使用率
				 
				 diskBean.setDisk_status("1");
				 
				 //容量
				 diskBean.setDisk_active_use("0.00");
				 
				 //设置活动容量
				 diskBean.setDisk_active_size(NumberTools.toKBSize(this.getViewTable().getString("DISK_SIZE"),2));
			 
				 diskBean.insert();
			 }

			 else if (getViewTable().getRecord().getRecordType() == 2) {
				 
				//磁盘名称校验
				 this.dataExistExchange("LEO_DISK_INFO", "DISK_NAM", "磁盘名称已经存在！");
				 
				 diskBean.updateByWhere("where DISK_ID='"+getViewTable().getString("DISK_ID")+"'");
				 
			 }

			 else if (getViewTable().getRecord().getRecordType() == 3) {
				 
				 //磁盘资源
				 this.dataRelaException("LEO_RES_INFO", "DISK_ID", "存在被使用的资源，请优先删除资源！");
				 
				 diskBean.deleteByWhere("where DISK_ID='"+getViewTable().getString("DISK_ID")+"'");
			 }

		 }
		 this.setSuccess("数据操作成功！");

	 }
	 
	 /**
	  * 重新初始化信息
	  */
	 public void instData(){
		 
		 String diskId = "";
		 
		 if(this.getViewTable().next()){
			 diskId = this.getViewTable().getString("diskIdBak");
		 }
		 
		 DiskExcutor de = new DiskExcutor(diskId);
		 de.instSize();
		 de.excute();
		 
		 this.setSuccess("磁盘信息重置成功！");
		 
	 }
	 
}
